<template>
    <div class="minimal-register">
      <div class="register-card">
        <h2 class="register-title">添加用户</h2>
        <el-form 
          ref="formRef"
          :model="registerForm"
          :rules="registerRules"
          class="register-form"
          label-width="80px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item label="权限" prop="power">
            <el-select
              v-model="registerForm.power"
              placeholder="请选择用户权限"
              style="width: 100%"
            >
              <el-option label="管理员" value="admin" />
              <el-option label="普通用户" value="user" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="卡号" prop="cardId">
            <el-input
              v-model="registerForm.cardId"
              placeholder="请输入用户卡号"
              prefix-icon="CreditCard"
              size="large"
            />
          </el-form-item>
          
          <el-button
            type="primary"
            size="large"
            class="register-btn"
            @click="submitForm"
            :loading="loading"
          >
            添加用户
          </el-button>

        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import axios from 'axios'
  
  const router = useRouter()
  
  const registerForm = reactive({
    username: '',
    password: '',
    confirmPassword: '',
    power: '',
    cardId: ''
  })
  
  const validatePassword = (rule, value, callback) => {
    if (value !== registerForm.password) {
      callback(new Error('两次输入的密码不一致'))
    } else {
      callback()
    }
  }
  
  const registerRules = reactive({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      
    ],
    confirmPassword: [
      { required: true, message: '请再次输入密码', trigger: 'blur' },
      { validator: validatePassword, trigger: 'blur' }
    ],
    power: [
      { required: true, message: '请选择用户权限', trigger: 'change' }
    ],
    cardId: [
      { required: true, message: '请输入用户卡号', trigger: 'blur' }
    ]
  })
  
  const loading = ref(false)
  const formRef = ref(null)
  
  const submitForm = () => {
    formRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        try {
          // 准备发送的数据（去掉确认密码字段）
          const { confirmPassword, ...submitData } = registerForm
          
          const response = await axios.post('/api/user/insertUser', submitData)
          
          if (response.data.code === 0) {
            ElMessage.success(response.data.message || '注册成功')
          } else {
            ElMessage.error(response.data.message || '注册失败')
          }
        } catch (error) {
          ElMessage.error(error.response?.data?.message || '请求失败，请稍后重试')
        } finally {
          loading.value = false
        }
      }
    })
  }
  </script>
  
  <style scoped>
  .minimal-register {
    display: flex; /* 确保是弹性容器 */
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
  }
  
  .register-card {
    width: 500px;
    padding: 40px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    max-width: 100%; /* 确保在小屏幕上不会超出视口 */
    overflow: auto; /* 防止内容溢出 */
  }
  
  .register-title {
    text-align: center;
    margin-bottom: 30px;
    color: #333;
    font-size: 20px;
    font-weight: normal;
  }
  
  .register-form :deep(.el-form-item) {
    margin-bottom: 20px;
  }
  
  .register-btn {
    width: 100%;
    margin-top: 10px;
  }
  
  .login-link {
    margin-top: 16px;
    text-align: center;
    font-size: 14px;
    color: #606266;
  }
  
  .login-link a {
    color: #409eff;
    text-decoration: none;
    margin-left: 4px;
  }
  
  .login-link a:hover {
    text-decoration: underline;
  }
  </style>